#include "addbookdialog.h"

#include <QMessageBox>
#include <QSettings>
#include <QStyle>

#include "ui_addbookdialog.h"

AddBookDialog::AddBookDialog(QWidget *parent)
    : QDialog(parent), ui(new Ui::AddBookDialog) {
    ui->setupUi(this);

    this->setWindowTitle("添加新的图书");
    // 设置窗口大小以及图标
    setFixedSize(256, 320);
    QStyle *style = QApplication::style();
    QIcon picture = style->standardIcon(QStyle::SP_DesktopIcon);
    setWindowIcon(picture);
}

AddBookDialog::~AddBookDialog() { delete ui; }

void AddBookDialog::on_confirm_clicked() {
    QStringList stringList;
    stringList.append(ui->name->text());
    stringList.append(ui->number->text());
    stringList.append(ui->author->text());
    stringList.append(ui->date->text());
    stringList.append(ui->publish->text());
    stringList.append(ui->domain->text());
    stringList.append(ui->price->text());
    for (auto &string : stringList) {
        if (string.length() == 0) {
            QMessageBox::warning(this, "错误", "输入数据不能为空");
            return;
        }
    }
    QSettings bookSettings("./book.ini", QSettings::IniFormat);
    bookSettings.beginGroup("book");
    bookSettings.setValue("book_name", ui->name->text());
    bookSettings.setValue("book_number", ui->number->text());
    bookSettings.setValue("book_publish", ui->publish->text());
    bookSettings.setValue("book_author", ui->author->text());
    bookSettings.setValue("book_domain", ui->domain->text());
    bookSettings.setValue("book_date", ui->date->date().toString("yyyy-MM-dd"));
    bookSettings.setValue("book_price", ui->price->text().toFloat());
    bookSettings.endGroup();
    accept();
}

void AddBookDialog::on_cancel_clicked() { reject(); }
